package com.hnkywl.sts.quartz;

import com.hnkywl.sts.service.taskdeal.DoKhGdqcOrderTimeSync;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.scheduling.quartz.CronTriggerBean;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.ParseException;

/**
 * Created by Jerry on 14-2-7.
 */
public class KhGdqcOrderTimeSyncJob extends QuartzJobBean {
    // 定义日志
    private static Log log = LogFactory.getLog(PlanImprtJob.class);
    private DoKhGdqcOrderTimeSync doKhGdqcOrderTimeSync;
    private Scheduler scheduler;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    }

    private void executeGdTimeJob() {
        log.info("khGdqcOrderTimeSyncJob 开始执行............");
        doKhGdqcOrderTimeSync.doGdqcSyncForOrderTime();
        log.info("khGdqcOrderTimeSyncJob 执行结束............");
    }

    public void updateNotificationInterval(String hours, String minutes, String seconds) throws SchedulerException, ParseException {
        String triggerName = "logAdd2FARSTaskJobTrigger";
        // 得到trigger
        CronTriggerBean trigger = (CronTriggerBean) scheduler.getTrigger(triggerName, Scheduler.DEFAULT_GROUP);
        // 得到cron expression
        if (trigger != null) {
            String cronExpression = getCronExpressionFromDB(hours, minutes, seconds);
            String oldCronExpression = trigger.getCronExpression();
            if (!oldCronExpression.equals(cronExpression)) {
                // 设置trigger的时间规则
                trigger.setCronExpression(cronExpression);
                // 重置job
                scheduler.rescheduleJob(triggerName, Scheduler.DEFAULT_GROUP, trigger);
            }
            System.out.println(trigger.getCronExpression());
        }
    }

    public String getCronExpressionFromDB(String hours, String minutes, String seconds) {
        if (hours == null)
            hours = "8";
        if (minutes == null)
            minutes = "0";
        if (seconds == null)
            seconds = "0";
        String dbCronExpression = seconds + " " + minutes + " " + hours + " ? * *";
        return dbCronExpression;
    }

    public DoKhGdqcOrderTimeSync getDoKhGdqcOrderTimeSync() {
        return doKhGdqcOrderTimeSync;
    }

    public void setDoKhGdqcOrderTimeSync(DoKhGdqcOrderTimeSync doKhGdqcOrderTimeSync) {
        this.doKhGdqcOrderTimeSync = doKhGdqcOrderTimeSync;
    }

    public Scheduler getScheduler() {
        return scheduler;
    }

    public void setScheduler(Scheduler scheduler) {
        this.scheduler = scheduler;
    }
}
